﻿using Common.Communal;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Product.API.Write.Applications.Commands.Upload;

namespace Product.API.Write.Controllers
{
    /// <summary>
    /// 上传文件API
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class UploadController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mediator">中介者</param>
        public UploadController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file">上传文件</param>
        /// <returns>返回地址</returns>
        [HttpPost("UploadImg")]
        public Task<ApiResult<string>> UploadImg(IFormFile file)
        {
            UploadImgCommand command = new UploadImgCommand();
            command.file = file;
            return mediator.Send(command);
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="file">上传文件</param>
        /// <returns>返回地址</returns>
        [HttpPost("UploadCompanyFile")]
        public Task<ApiResult<string>> UploadCompanyFile(IFormFile file)
        {
            UploadFileCommand command = new UploadFileCommand();
            command.file = file;
            return mediator.Send(command);
        }

        /// <summary>
        /// 文件删除
        /// </summary>
        /// <param name="url">上传文件</param>
        /// <returns>返回地址</returns>
        [HttpPost("DeleteFile")]
        public Task<ApiResult<int>> DeleteFile(string url)
        {
            DeleteFileCommand command = new DeleteFileCommand();
            command.url = url;  
            return mediator.Send(command);
        }


    }
}
